﻿using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using InvestmentIntelligence.SchedulingService.Infrastructure;
using InvestmentIntelligence.TradingData.DataProcessing.DataResolvers;
using InvestmentIntelligence.TradingData.Handlers.Handlers.MissedDataHandlers;
using InvestmentIntelligence.TradingData.Handlers.PostHandlersProcessing;
using InvestmentIntelligence.TradingData.Handlers.PostHandlersProcessing.Trades;
using InvestmentIntelligence.TradingData.TradingDataModels;

namespace InvestmentIntelligence.TradingData.Handlers.Mus
{
    [Export(typeof(IScheduledTaskFactory))]
    [ExportMetadata("TaskName", "PostProcessingTrades")]
    public class B1PostProcessingTradesHandlerFactory : IScheduledTaskFactory
    {
        private readonly ScheduledTaskParameter _fixTradePrice = new StringScheduledTaskParameter(
            "fixTradePrice", "specifies whether to fix trade price");

        private readonly ScheduledTaskParameter _fixTradeSizeBookIds = new IntegerArrayScheduledTaskParameter(
           "fixTradeSizeBookIds", "specifies a book ids where to calculate trade.Size");

        public IEnumerable<ScheduledTaskParameter> GetParameters()
        {
            yield return _fixTradePrice;
            yield return _fixTradeSizeBookIds;
        }

        public IScheduledTask CreateTask(CreateTaskParams createTaskParams, IDictionary<ScheduledTaskParameter, string> arguments)
        {
            bool fixTradePrice;
            bool.TryParse(_fixTradePrice.Get(arguments, ""), out fixTradePrice);
            var bookIds = _fixTradeSizeBookIds.Get(arguments, new List<int>());
            var securityInfoMapper = new SecurityInfoMapper();

            var postProcessing = new List<PostProcessing<TradingDataTradeDto>>
            {
                new CurrencyMultiplierPostProcessing(),
                new PopulateMissedFxRatesInTradesPostProcessing(),
                new TradePriceConsistencyPostProcessing(securityInfoMapper, fixTradePrice),
                new PopulateTradeSizePostProcessing(securityInfoMapper, bookIds.ToList())
            };
            return new PopulateMissedTradeDataHandler(MusBootstrapper.Instance.SecurityResolver, postProcessing);
        }

        public IEnumerable<System.Tuple<ScheduledTaskParameter, string>> ValidateArguments(IDictionary<ScheduledTaskParameter, string> arguments)
        {
            yield break;
        }
    }
}